package com.kl.controller;


import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.Menu;
import com.kl.entity.Staff;
import com.kl.service.IMenuService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService iMenuService;

    @ApiOperation(value = "菜单表树状图3")
    @GetMapping("/findMenuJson")
    public Result getInfo(HttpServletRequest request){
        HttpSession session = request.getSession();
        Staff staff = (Staff)session.getAttribute("staff");
        List<Map<String,Object>>mapList =  iMenuService.findMenuJson(staff.getId());
        return Result.build(ConstantEnum.SUCCESS).setResult(mapList);
    }

    @ApiOperation(value = "菜单表树状图2")
    @GetMapping("/getMenuZtree")
    public  Result  getMenuZtree(){
        List<Map<String, Object>>  result = iMenuService.getMenuZtree();
        return Result.build(ConstantEnum.SUCCESS).setResult(result);
    }

    @ApiOperation(value = "增加菜单表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menu", value = "菜单表实体类", required = true,
                    paramType = "body", dataTypeClass = Menu.class)

    })
    @PostMapping("/addMenuInfo")
    public Result addMenuInfo(@RequestBody Menu menu){
        Result result = iMenuService.addMenuInfo(menu);
        return result;
    }

    @ApiOperation(value = "菜单表修改回显接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mid", value = "菜单表id", required = true,
                    paramType = "query", dataTypeClass = Integer.class)
    })
    @GetMapping("/toUpdate")
    public Result toUpdate(@RequestParam Integer mid){
        Menu menu = iMenuService.selectMenuById(mid);

        return Result.build(ConstantEnum.SUCCESS).setResult(menu);
    }


    @ApiOperation(value = "菜单表修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menu", value = "菜单表实体类", required = true,
                    paramType = "body", dataTypeClass = Menu.class)
    })
    @PutMapping("/updateMenu")
    public Result updateMenu(@RequestBody Menu menu){
        Result result = iMenuService.updateMenu(menu);
        return result;
    }

    @ApiOperation(value = "菜单表删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单表id", required = true,
                    paramType = "quest", dataTypeClass = Integer.class)
    })
    @DeleteMapping("/deleteMenu")
    public Result deleteMenu(@RequestParam Integer mid){
        Result result = iMenuService.deleteMenu(mid);
        return result;
    }

    @ApiOperation(value = "菜单表树状图1")
    @GetMapping("/getMenuZtreeChecked")
    public  Result getMenuZtreeChecked(@RequestParam Integer rid){

        List<Map<String, Object>> list = iMenuService.getMenuZtreeChecked(rid);
        System.out.println(list);
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }
}
